<template>
  <div class="box">
    <p>用户信息管理系统</p>
    <el-form
      :model="ruleForm"
      status-icon
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <img :src="img" alt="" width="100px" height="100px" class="avatar" />
      <el-form-item prop="username" class="item">
        <span slot="label" style="color: #fff">用户名</span>
        <el-input
          type="text"
          v-model="ruleForm.username"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item prop="password" class="item">
        <span slot="label" style="color: #fff">密码</span>
        <el-input
          type="password"
          v-model="ruleForm.password"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item prop="captcha" class="item">
        <span slot="label" style="color: #fff">验证码</span>
        <el-input type="text" v-model="ruleForm.captcha"></el-input>
        <img :src="captcha" alt="" class="captcha" @click="changeCaptcha" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >登录</el-button
        >
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
export default {
  data() {
    return {
      captcha: '',

      ruleForm: {
        password: '',
        username: '',
        captcha: '',
        key: '',
      },
      rules: {
        password: [{ validator: this.checkPassword, trigger: 'blur' }],
        username: [{ validator: this.checkUsername, trigger: 'blur' }],
        captcha: [{ validator: this.checkCaptcha, trigger: 'blur' }],
      },
      img: require('../assets/avtar.jpg'),
    }
  },
  created() {
    // 请求验证码
    this.changeCaptcha()
  },
  methods: {
    checkUsername(rule, value, callback) {
      if (value === '') {
        callback(new Error('用户名不能为空'))
      } else {
        callback()
      }
    },
    checkPassword(rule, value, callback) {
      if (value === '') {
        callback(new Error('密码不能为空'))
      } else {
        callback()
      }
    },
    checkCaptcha(rule, value, callback) {
      if (value === '') {
        callback(new Error('验证码不能为空'))
      } else {
        callback()
      }
    },
    // 将对象转成字符串的方法
    objToStr(obj) {
      let arr = []
      for (let attr in obj) {
        arr.push(attr + '=' + obj[attr])
      }
      return arr.join('&')
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let data = this.objToStr(this.ruleForm)
          // console.log(data)
          this.$http.post('/api/login', data).then((res) => {
            // console.log(res)
            if (res.errNo === 0 && res.message === 'success') {
              //跳转到首页
              this.$router.push('/')
            }
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    changeCaptcha() {
      this.$http.get('/api/getCaptcha/math').then((res) => {
        // console.log(res)
        this.captcha = res.img
        this.ruleForm.key = res.key
      })
    },
  },
}
</script>
<style lang="scss" scoped>
.box {
  width: 100%;
  height: 100vh;
  background: url('../assets/login.gif') no-repeat center;
  background-size: cover;
  display: flex;
  justify-content: center;
  align-items: center;
  p {
    position: absolute;
    left: 50%;
    top: 0;
    transform: translateX(-50%);
    font-family: 'Audiowide';
    color: rgb(34, 89, 153);
    font-size: 2.5em;
    color: #fff;
    animation: Glow 1.5s ease infinite alternate;
  }
  .avatar {
    position: absolute;
    top: -70px;
    left: 50%;
    transform: translateX(-50%);
    border-radius: 50%;
  }
  form {
    width: 400px;
    padding-right: 45px;
    padding-top: 35px;
    box-sizing: border-box;
    // color: #fff;
    background-color: rgba(34, 89, 153, 0.9);
    position: relative;
    // z-index: 1000;
    .captcha {
      position: relative;
      top: 15px;
    }
    .item .el-form-item__label {
      color: #fff;
    }
  }
}
@keyframes Glow {
  from {
    text-shadow: 0 0 10px #fff, 0 0 20px #fff, 0 0 30px #fff,
      0 0 40px rgba(34, 89, 153, 0.9), 0 0 70px rgba(34, 89, 153, 0.9),
      0 0 80px rgba(34, 89, 153, 0.9), 0 0 100px rgba(34, 89, 153, 0.9),
      0 0 150px rgba(34, 89, 153, 0.9);
  }
  to {
    text-shadow: 0 0 5px #fff, 0 0 10px #fff, 0 0 15px #fff,
      0 0 20px rgba(34, 89, 153, 0.9), 0 0 35px rgba(34, 89, 153, 0.9),
      0 0 40px rgba(34, 89, 153, 0.9), 0 0 50px rgba(34, 89, 153, 0.9),
      0 0 75px rgba(34, 89, 153, 0.9);
  }
}
</style>